﻿using System;
using System.Text;
using System.Web.UI;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Collections.Specialized;
using Microsoft.SharePoint.Security;
using System.Security.Permissions;
using Microsoft.SharePoint.MobileControls;
using Microsoft.SharePoint.WebControls;

namespace Voodoo.SharePoint.ReadUnreadMarks
{
    /// <summary>
    /// Simple wrapper so that a list will not blow up for a mobile renderer
    /// </summary>
    public class ReadUnreadMobileFieldControl : SPMobileBaseFieldControl
    {

        private ReadUnreadField _ParentField = null;
        private ReadUnreadFieldValue _Value = null;

        public ReadUnreadMobileFieldControl(ReadUnreadField parentField)
        {
            _ParentField = parentField;
        }

        public override object Value
        {
            get
            {
                return _Value;
            }
            set
            {
                if (value is string)
                {
                    _Value = new ReadUnreadFieldValue(value);
                }
                else
                {
                    if (value is ReadUnreadFieldValue)
                    {
                        _Value = (ReadUnreadFieldValue)value;
                    }
                    else
                    {
                        if (null == value)
                        {
                            _Value = null;
                        }
                        else
                        {
                            throw new InvalidCastException();
                        }
                    }
                }

            }
        }

        public override void Focus()
        {
            EnsureChildControls();
        }

        protected override void CreateChildControls()
        {
            if (SPControlMode.Display == this.ControlMode)
            {
                if (Context.User.Identity.IsAuthenticated)
                {
                    //We dont mark folder items by reading them
                    if (null == this.ListItem.Folder)
                    {
                        //If we are in display mode we need to set the read mark for this user
                        SPUser currentUser = UtilityMethods.EnsureUser(System.Web.HttpContext.Current.User.Identity.Name);
                        try
                        {
                            UtilityMethods.MarkItemAsRead(currentUser, this.ListItem, _ParentField);
                        }
                        catch (Exception processingError)
                        {
                            System.Diagnostics.Trace.WriteLine(processingError.Message);
                        }
                    }
                }
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            //TODO We dont render anything for a mobile control
        }
    }
}
